import VueRouter from "vue-router";
import Vue from 'vue'

let head = document.getElementsByTagName('head');
let meta = document.createElement('meta');
meta.name = 'referrer';
meta.content = 'no-referrer';
head[0].appendChild(meta);

let fromPath = '';
let epShowType = 'view'
export {fromPath, epShowType}

let Router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: "/",
            name: "首页",
            component: () => import("../pages/index.vue"),
            children: [
                {
                    path: "/mgHome",
                    name: "mgHome",
                    meta: {
                        title: ''
                    },
                    component: () => import("@/pages/manga/mgHome.vue"),
                },
                {
                    path: "/mgEp:id?",
                    name: "mgEp",
                    meta: {
                        title: ''
                    },
                    component: () => import("@/pages/manga/mgEp.vue"),
                },
                {
                    path: "/mgEp/dt:id?",
                    name: "mgEpDt",
                    meta: {
                        title: ''
                    },
                    component: () => import("@/pages/manga/mgEpDt.vue"),
                },
                {
                    path: "/mineMg",
                    name: "mineMg",
                    meta: {
                        title: '',
                    },
                    component: () => import("@/pages/manga/mineMg.vue"),
                },
                {
                    path: "/cnTeam",
                    name: "cnTeam",
                    meta: {
                        title: ''
                    },
                    component: () => import("@/pages/cnTeam/cnTeamTable.vue"),
                },
            ]
        },
        {
            path: "/404",
            name: "404",
            meta: {
                title: '404'
            },
            component: () => import("../pages/error/404.vue"),
        },
        {
            path: "/:catchAll(.*)",
            redirect: "/404",
        },
    ],
});

/**
 * 处理 epShowType
 * @param from
 * @param to
 */
function handleEpShowType(from, to) {
    // 先从本地获取epShowType
    epShowType = localStorage.getItem('epShowType') || 'view'
    // 仅从我的漫画到ep详情页时epShowType为edit，其他跳转至ep详情页都为view视图
    if (from.path === "/mineMg" && to.path === "/mgEp") {
        epShowType = 'edit'
    } else if (to.path === "/mgEp" && (from.path !== '/mgEp/dt' && from.path !== '/')) {
        epShowType = 'view'
    }
    localStorage.setItem('epShowType', epShowType)
}

Router.beforeEach((to, from, next) => {
    fromPath = from.path;
    handleEpShowType(from, to);
    if (to.path === "/") {
        next('/mgHome');
    } else {
        next();
    }
})

Router.afterEach((to) => {
    document.title = 'We-Roast' + to.meta.title;
});


/* 解决 push/replace 重复报错 */
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location, onResolve, onReject) {

    if (onResolve || onReject)

        return originalPush.call(this, location, onResolve, onReject);

    return originalPush.call(this, location).catch((err) => err);

};

Vue.use(VueRouter)

export default Router;
